package com.sxzbxc.weixin.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.sxzbxc.weixin.entity.DicCompanies;
import com.sxzbxc.weixin.entity.DicLineinfo;
import com.sxzbxc.weixin.entity.DicMelfunction;
import com.sxzbxc.weixin.entity.DicSubstation;
import com.sxzbxc.weixin.entity.SysUser;
import com.sxzbxc.weixin.pojo.AjaxJson;
import com.sxzbxc.weixin.service.DicCompaniseServiceI;
import com.sxzbxc.weixin.service.DicLineinfoServiceI;
import com.sxzbxc.weixin.service.DicMelfunctionServiceI;
import com.sxzbxc.weixin.service.DicSubstationServiceI;
import com.sxzbxc.weixin.service.SysRoleServiceI;
import com.sxzbxc.weixin.service.SysUserServiceI;


/**
 * 管理中心模块
 * @author Tom
 *
 */
@Controller
@RequestMapping("systemController")
public class SystemController {

	@Autowired
	private DicMelfunctionServiceI dicMelfunctionService;
	
	@Autowired
	private DicCompaniseServiceI dicCompaniseService;
	
	@Autowired
	private DicLineinfoServiceI dicLineinfoService;
	
	@Autowired
	private DicSubstationServiceI dicSubstationService;
	
	@Autowired
	private SysUserServiceI sysUserService;
	
	@Autowired
	private SysRoleServiceI sysRoleService;
	

	/**
	 * 进入管理中心页面
	 * @return
	 */
	@RequestMapping(params = "list")
	public ModelAndView list(){
		
		return new ModelAndView("admin/adminList");
	}
	
	/**
	 * 进入单位信息页面
	 * @return
	 */
	@RequestMapping(params = "toCompanise")
	public ModelAndView toCompanies(){
		
		return new ModelAndView("admin/companiseSelect");
	}
	
	/**
	 * 进入线路信息页面
	 * @return
	 */
	@RequestMapping(params = "toLineinfo")
	public ModelAndView toLineinfo(){
		
		return new ModelAndView("admin/lineinfoSelect");
	}
	/**
	 * 进入故障类型页面
	 * @return
	 */
	@RequestMapping(params = "toMelfunction")
	public ModelAndView toMelfunction(){

		return new ModelAndView("admin/melfunctionSelect");
	}
	
	/**
	 * 进入变电所信息页面
	 * @return
	 */
	@RequestMapping(params = "toSubsitation")
	public ModelAndView toSubsitation(){
		
		return new ModelAndView("admin/subsitationSelect");
	}
	
	/**
	 * 进入用户管理页面
	 * @return
	 */
	@RequestMapping(params = "toUser")
	public ModelAndView toUser(){
		
		return new ModelAndView("admin/userSelect");
	}
	
	@RequestMapping(params = "toWork")
	public ModelAndView toWork(){
		return new ModelAndView("work/workOrder");
	}
	/**
	 * 进入单位信息添加页面
	 * @return
	 */
	@RequestMapping(params = "toAddCompanise")
	public ModelAndView toAddCompanise(){
		
		return new ModelAndView("admin/companiseAdd");
	}
	
	/**
	 * 进入线路信息添加页面
	 * @return
	 */
	@RequestMapping(params = "toAddLineinfo")
	public ModelAndView toAddLineinfo(){
		
		return new ModelAndView("admin/lineinfoAdd");
	}
	
	/**
	 * 进入故障信息添加页面
	 * @return
	 */
	@RequestMapping(params = "toAddMelfunction")
	public ModelAndView toAddMelfunction(){
		
		return new ModelAndView("admin/melfunctionAdd");
	}
	
	/**
	 * 进入变电站信息添加页面
	 * @return
	 */
	@RequestMapping(params = "toAddSubsitation")
	public ModelAndView toAddSubsitation(){
		
		return new ModelAndView("admin/subsitationAdd");
	}
	
	/**
	 * 进入用户信息添加页面
	 * @return
	 */
	@RequestMapping(params = "toAddUser")
	public ModelAndView toAddUser(){
		
		return new ModelAndView("admin/userAdd");
	}
	
	/**
	 * 进入单位信息列表
	 * @return
	 */
	@RequestMapping(params = "companiseList")
	public ModelAndView companiseList(){
		
		return new ModelAndView("admin/companiseList");
	}
	
	@RequestMapping(params = "companiseDatagrid")
	@ResponseBody
	public AjaxJson companiseDatagrid(){
		AjaxJson j = new AjaxJson();
		List<DicCompanies> loadAll = dicCompaniseService.loadAll();
//		for (DicCompanies dicCompanies : loadAll) {
//			if(dicCompanies.getComStatus()!=1){
//				loadAll.remove(dicCompanies);
//			}
//		}
		j.setObj(loadAll);
		j.setSuccess(true);
		return j;
	}
	
	/**
	 * 进入线路信息列表
	 * @return
	 */
	@RequestMapping(params = "lineinfoList")
	public ModelAndView lineinfoList(){
		
		return new ModelAndView("admin/lineinfoList");
	}
	
	@RequestMapping(params = "lineinfoDatagrid")
	@ResponseBody
	public AjaxJson lineinfoDatagrid(){
		AjaxJson j = new AjaxJson();
		List<DicLineinfo> loadAll = dicLineinfoService.loadAll();
//		for (DicLineinfo dicLineinfo : loadAll) {
//			if(dicLineinfo.getLineStatus()!=1){
//				loadAll.remove(dicLineinfo);
//			}
//		}
		j.setObj(loadAll);
		j.setSuccess(true);
		return j;
	}
	
	/**
	 * 进入故障信息列表
	 * @return
	 */
	@RequestMapping(params = "melfunctionList")
	public ModelAndView melfunctionList(){
		return new ModelAndView("admin/melfunctionList");
	}
	
	@RequestMapping(params = "melfunctionDatagrid")
	@ResponseBody
	public AjaxJson melfunctionDatagrid(){
		AjaxJson j = new AjaxJson();
		List<DicMelfunction> loadAll = dicMelfunctionService.loadAll();
//		ListIterator<DicMelfunction> iterator = loadAll.listIterator();
//		while (iterator.hasNext()) {
//			DicMelfunction dicMelfunction = iterator.next();
//			if (dicMelfunction.getDicStatus()!=1) {
//				iterator.remove();
//			}
//		}
		j.setObj(loadAll);
		j.setSuccess(true);
		return j;
	}
	
	/**
	 * 进入变电站信息列表
	 * @return
	 */
	@RequestMapping(params = "subsitationList")
	public ModelAndView subsitationList(){
		
		return new ModelAndView("admin/subsitationList");
	}
	
	@RequestMapping(params = "subsitationDatagrid")
	@ResponseBody
	public AjaxJson subsitationDatagrid(){
		AjaxJson j = new AjaxJson();
		List<DicSubstation> loadAll = dicSubstationService.loadAll();
		for (DicSubstation dicSubstation : loadAll) {
			if(dicSubstation.getSubStatus()!=1){
				loadAll.remove(dicSubstation);
			}
		}
		j.setObj(loadAll);
		j.setSuccess(true);
		return j;
	}
	
	/**
	 * 进入用户信息列表
	 * @return
	 */
	@RequestMapping(params = "userList")
	public ModelAndView userList(){
		
		return new ModelAndView("admin/userList");
	}
	
	@RequestMapping(params = "userDatagrid")
	@ResponseBody
	public AjaxJson userDatagrid(){
		AjaxJson j = new AjaxJson();
		List<SysUser> loadAll = sysUserService.loadAll();
		j.setObj(loadAll);
		j.setSuccess(true);
		return j;
	}
	
	/**
	 * 进入单位信息更新页面
	 * @return
	 */
	@RequestMapping(params = "toUpdateCompanise")
	public ModelAndView toUpdateCompanise(){
		
		return new ModelAndView("admin/companiseUpdate");
	}
	
	@RequestMapping(params = "updateCompaniseDatagrid")
	@ResponseBody
	public AjaxJson updateCompaniseDatagrid(String id){
		AjaxJson j = new AjaxJson ();
		DicCompanies selectByPrimaryKey = dicCompaniseService.selectByPrimaryKey(id);
		j.setObj(selectByPrimaryKey);
		j.setSuccess(true);
		return j;
	}
	
	/**
	 * 进入线路信息更新页面
	 * @return
	 */
	@RequestMapping(params = "toUpdateLineinfo")
	public ModelAndView toUpdateLineinfo(){
		
		return new ModelAndView("admin/lineinfoUpdate");
	}
	
	@RequestMapping(params = "updateLineinfoDatagrid")
	@ResponseBody
	public AjaxJson updateLineinfoDatagrid(String id){
		AjaxJson j = new AjaxJson ();
		DicLineinfo selectByPrimaryKey = dicLineinfoService.selectByPrimaryKey(id);
		j.setObj(selectByPrimaryKey);
		j.setSuccess(true);
		return j;
	}
	
	/**
	 * 进入故障信息更新页面
	 * @return
	 */
	@RequestMapping(params = "toUpdateMelfunction")
	public ModelAndView toUpdateMelfunction(HttpServletRequest request ,  String id){
		ModelAndView rs = new ModelAndView();
		rs.addObject("id", id);
		rs.setViewName("admin/melfunctionUpdate");
		return rs;
	}
	
	@RequestMapping(params = "updateMelfunctionDatagrid")
	@ResponseBody
	public AjaxJson updateMelfunctionDatagrid(String id){
		AjaxJson j = new AjaxJson ();
		DicMelfunction selectByPrimaryKey = dicMelfunctionService.selectByPrimaryKey(id);
		j.setObj(selectByPrimaryKey);
		j.setSuccess(true);
		return j;
	}
	
	/**
	 * 进入变电站信息更新页面
	 * @return
	 */
	@RequestMapping(params = "toUpdateSubsitation")
	public ModelAndView toUpdateSubsitation(){
		
		return new ModelAndView("admin/subsitationUpdate");
	}
	
	@RequestMapping(params = "updateSubsitationDatagrid")
	@ResponseBody
	public AjaxJson updateSubsitationDatagrid(String id){
		AjaxJson j = new AjaxJson ();
		DicSubstation selectByPrimaryKey = dicSubstationService.selectByPrimaryKey(id);
		j.setObj(selectByPrimaryKey);
		j.setSuccess(true);
		return j;
	}
	
	/**
	 * 进入用户信息更新页面
	 * @return
	 */
	@RequestMapping(params = "toUpdateUser")
	public ModelAndView toUpdateUser(){
		
		return new ModelAndView("admin/userUpdate");
	}
	
	@RequestMapping(params = "updateUserDatagrid")
	@ResponseBody
	public AjaxJson updateUserDatagrid(String id){
		AjaxJson j = new AjaxJson ();
		SysUser selectByPrimaryKey = sysUserService.selectByPrimaryKey(id);
		j.setObj(selectByPrimaryKey);
		j.setSuccess(true);
		return j;
	}
	
	/**
	 * 添加单位信息任务
	 * @return
	 */
	@RequestMapping(params = "doAddCompanise")
	@ResponseBody
	public AjaxJson doAddCompanise(HttpServletRequest request , HttpServletResponse response, DicCompanies dicCompanies){
		AjaxJson j = new AjaxJson();
		try {
			dicCompaniseService.insertSelective(dicCompanies);
			j.setSuccess(true);
		} catch (Exception e) {
			j.setSuccess(false);
		}
		return j ;
	}
	
	/**
	 * 添加线路信息任务
	 * @return
	 */
	@RequestMapping(params = "doAddLineinfo")
	@ResponseBody
	public AjaxJson doAddLineinfo(HttpServletRequest request , HttpServletResponse response,String id , String lineCode, String lineName, int lineStatus){
		AjaxJson j = new AjaxJson();
		DicLineinfo dicLineinfo = dicLineinfoService.selectByPrimaryKey(id);
		try {
			dicLineinfo.setLineCode(lineCode);
			dicLineinfo.setLineName(lineName);
			dicLineinfo.setLineStatus(lineStatus);
			dicLineinfoService.insertSelective(dicLineinfo);
		} catch (Exception e) {
			e.printStackTrace();
			j.setSuccess(false);
		}
		j.setSuccess(true);
		return j ;
	}
	
	/**
	 * 添加故障信息任务
	 * @return
	 */
	@RequestMapping(params = "doAddSubsitation")
	@ResponseBody
	public AjaxJson doAddSubsitation(HttpServletRequest request , HttpServletResponse response,DicSubstation dicSubstation){
		AjaxJson j = new AjaxJson();
		try {
			dicSubstationService.insertSelective(dicSubstation);
			j.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			j.setSuccess(false);
		}
		return j ;
	}
	
	/**
	 * 添加故障信息任务
	 * @return
	 */
	@RequestMapping(params = "doAddMelfunction")
	@ResponseBody
	public AjaxJson doAddMelfunction(HttpServletRequest request , HttpServletResponse response,DicMelfunction dicMelfunction){
		AjaxJson j = new AjaxJson();
		try {
			dicMelfunctionService.insertSelective(dicMelfunction);
			j.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			j.setSuccess(false);
		}
		return j ;
	}
	
	
	/**
	 * 更新单位信息任务
	 * @return
	 */
	@RequestMapping(params = "doUpdateCompanise")
	@ResponseBody
	public AjaxJson doUpdateCompanise(HttpServletRequest request , HttpServletResponse response,DicCompanies dicCompanies){
		AjaxJson j = new AjaxJson();
		try {
			dicCompaniseService.updateByPrimaryKeySelective(dicCompanies);
			j.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			j.setSuccess(false);
		}
		return j ;
	}
	
	/**
	 * 更新线路信息任务
	 * @return
	 */
	@RequestMapping(params = "doUpdateLineinfo")
	@ResponseBody
	public AjaxJson doUpdateLineinfo(HttpServletRequest request , HttpServletResponse response,String id , String lineCode, String lineName, Integer lineStatus){
		AjaxJson j = new AjaxJson();
		DicLineinfo dicLineinfo = dicLineinfoService.selectByPrimaryKey(id);
		dicLineinfo.setLineCode(lineCode);
		dicLineinfo.setLineName(lineName);
		dicLineinfo.setLineStatus(lineStatus);
		try {
			dicLineinfoService.updateByPrimaryKeySelective(dicLineinfo);
			j.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			j.setSuccess(false);
		}
		return j ;
	}
	
	/**
	 * 更新故障信息任务
	 * @return
	 */
	@RequestMapping(params = "doUpdateMelfunction")
	@ResponseBody
	public AjaxJson doUpdateMelfunction(HttpServletRequest request , HttpServletResponse response,String id , String dicMelCode, String dicMelName, int dicMelStatus){
		AjaxJson j = new AjaxJson();
		DicMelfunction dicMelfunction = dicMelfunctionService.selectByPrimaryKey(id);
		try {
			dicMelfunction.setDicMelcode(dicMelCode);
			dicMelfunction.setDicMelname(dicMelName);
			dicMelfunction.setDicStatus(dicMelStatus);
			dicMelfunctionService.updateByPrimaryKeySelective(dicMelfunction);
		} catch (Exception e) {
			// TODO: handle exception
		}
		j.setSuccess(true);
		return j ;
	}
	
	/**
	 * 更新变电站信息任务
	 * @return
	 */
	@RequestMapping(params = "doUpdateSubsitation")
	@ResponseBody
	public AjaxJson doUpdateSubsitation(HttpServletRequest request , HttpServletResponse response,
			DicSubstation dicSubstation){
			AjaxJson j = new AjaxJson();
			dicSubstationService.updateByPrimaryKeySelective(dicSubstation);
			j.setSuccess(true);
		return j ;
	}
	
	
	
	@RequestMapping(params = "userDepart")
	@ResponseBody
	public AjaxJson userDepart(HttpServletRequest request){
		AjaxJson j = new AjaxJson();
		Map <String , String > list = new HashMap<String , String>();
		List<DicCompanies> depart = dicCompaniseService.loadAll();
		for (DicCompanies dicCompanies : depart) {
			String comRegionName = dicCompanies.getComRegionName();
			String comPlaceName = dicCompanies.getComPlaceName();
			String depart1 = comRegionName+" "+ comPlaceName;
			String id = dicCompanies.getId();
			list.put(id, depart1);
		}
		j.setObj(list);
		j.setSuccess(true);
		return j;
	}
}
